<?php

class Admin_Profile_EditAction extends BlogAdminBaseAction
{
    public function executeRead(AgaviRequestDataHolder $rd)
    {
        $user = $this->initUser($rd);
        
        if (is_null($user)) {
            return array('Public', 'Error404Success');
        }
        
        return 'Input';
    }
    
    public function executeWrite(AgaviRequestDataHolder $rd)
    {
        $params = $rd->getParameters();
        /* @var $users_model Admin_UsersModel */
        $users_model = $this->getContext()->getModel('Users', 'Admin');
        $vm = $this->getContainer()->getValidationManager();
        $user = $this->initUser($rd);
        
        if (is_null($user)) {
            return array('Public', 'Error404Success');
        }
        
        $new_password = null;
        // update user password if required
        if ($user->user_type == 'plain' && isset($params['new_password_1'])) {
            $new_password = $params['new_password_1'];
        }
        
        $lo_website = strtolower($params['website']);
        if ($lo_website != '' && strpos($lo_website, 'http://') !== 0 && strpos($lo_website, 'https://') !== 0) {
            $params['website'] = 'http://'.$params['website'];
        }
        $users_model->updateUserData($user, $params['display_name'], $params['email'], 
            $params['website'], $new_password, isset($params['trusted']));
        
        return 'Success';
    }
    
    public function handleError(AgaviRequestDataHolder $rd)
    {
        $user = $this->initUser($rd);
        
        if ($user === false) {
            return array('Public', 'Error404Success');
        }
        
        return 'Error';
        
    }
    
    /**
     * Init User object
     *
     * @param AgaviRequestDataHolder $rd
     * @return User
     */
    protected function initUser(AgaviRequestDataHolder $rd)
    {
        $users_model = $this->getContext()->getModel('Users', 'Admin');
        $params = $rd->getParameters();
        $user = $users_model->getUser($params['user_id']);
        
        $this->setAttribute('user', $user);
        return $user;
        
    }
    
	/**
	 * Returns the default view if the action does not serve the request
	 * method used.
	 *
	 * @return     mixed <ul>
	 *                     <li>A string containing the view name associated
	 *                     with this action; or</li>
	 *                     <li>An array with two indices: the parent module
	 *                     of the view to be executed and the view to be
	 *                     executed.</li>
	 *                   </ul>
	 */
	public function getDefaultViewName()
	{
		return 'Input';
	}
}

?>
